//Implementation file - Ball.cpp

#include "Ball.h"
#include "math.h"

Ball::Ball(  ) {
        this->position = PV(0,0);
        this->radius = 10;
        this->direction = PV(random(5),random(5));
        this->circle = circle;
}

Ball::Ball(PV position, float radius, PV direction, Figure circle) {
        this->position = position;
        this->radius = radius;
        this->direction = direction;
        this->circle = circle;
}

Ball::randomBall() {

}

PV Ball::getPos(){
        return this->position;
}

PV Ball::getDir(){
        return this->direction;
}

float Ball::getRadius(){
        return this->radius;
}

Figure Ball::getCircle(){
        return this->circle;
}

void Ball::setPos( PV position ){
        this->position = position;
}

void Ball::setPos( float x , float y ){
        this->position.setx(x);
        this->position.sety(y);
}

void Ball::setDir( PV direction ){
        this->direction = direction;
}

void Ball::setRadius( float radius){
        this->radius = radius;
}

void Ball::setCircle( Figure circle ){
        this->circle = circle;
}

void Ball::avanza()  {
        float speed = sqrt(pow(direction.getx(),2) + pow(direction.gety(),2));
        position.setx(position.getx() + speed*cos(direction.alpha()));
        position.sety(position.gety() + speed*sin(direction.alpha()));
}

/*
 * With this method we try to approximate the circunference using a multi-polygon
 * drawing approach. Based on the radio size, we draw more or less polygons
 */

void Ball::draw(  ){
        Pen* pencil = new Pen(  );
        int sides = (int) radius * 5; //Selection of iterations
        float alpha = (2*M_PI)/sides;
        float longlong   = ( radius*M_PI*2 )/sides;

        pencil->setDir( 0 );
        pencil->setPos( position.getx(  )+radius,position.gety(  ) );
        pencil->setDir( M_PI/2 );

        Figure* figure = new Figure(  );
        for(int i=0;i<sides;i++){
                figure->getVertex()->insertaDespues(pencil->forward(longlong));
                pencil->turn(alpha);
            }

        circle = *figure;
        circle.draw();
};
